// pages/my/teacher/createClass/createClass.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'teacher_id',
      success: function (res) {
        that.setData({
          teacher_id: res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sure2create:function(){
    var that = this
    wx.request({
      url: base_url+'/addClass/',
      method: 'POST',
      data: {
        teacher_id: that.data.teacher_id,
        gradeText: that.data.gradeText,
        classText: that.data.classText
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data['flag'] == true) {
          if(res.data['type']=='1'){
            wx.showToast({
              title: '班级已存在',
              icon: 'loading',
              duration: 2000
            })
          }else{
            wx.setStorage({
              key: 'classDatas_lst',
              data: res.data['classDatas_lst'],
              success: function () {
                wx.navigateTo({
                  url: '../classManage/classManage',
                  success: function (res) {

                    wx.showToast({
                      title: '创建成功',
                      icon: 'ok',
                      duration: 3000
                    })
                  },
                  fail: function () {
                  }
                })
              }
            })
            
          }
          
        }else{
          wx.showToast({
            title: '输入错误',
            icon: 'loading',
            duration: 2000
          })
        }
      }
    })
  },
  bindInputGrade:function(e){
    this.setData({
      gradeText: e.detail.value
    })
  },
  bindInputClass:function(e){
    this.setData({
      classText: e.detail.value
    })
  },

})